package com.leonzx.base.controller.ureport;

import com.leonzx.base.config.pager.Page;
import com.leonzx.base.config.pager.PageInfo;
import com.leonzx.base.controller.base.BaseController;
import com.leonzx.base.entity.system.UreportFile;
import com.leonzx.base.service.system.UreportFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @Author:梁亮
 * @Description:
 * @Date: 2018/7/4 21:52
 */
@RestController
@RequestMapping("/ajax/report/query")
public class AjaxUreportQueryController extends BaseController {

    @Autowired
    private UreportFileService ureportFileService;

    @RequestMapping("/list")
    public Map<String, Object> list(UreportFile ureportFile, @PageInfo(pageSize = Page.PAGE_SIZE, orderBy = "update_time", orderDiv = Page.Sort.DESC) Page<UreportFile> page) {
        Map<String, Object> result = getMap();
        try {
            List<UreportFile> ureportFiles = ureportFileService.getAll(ureportFile);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");

            for (UreportFile uf : ureportFiles) {
                String createTimeStr = sdf.format(uf.getCreateTime());
                String updateTimeStr = sdf.format(uf.getUpdateTime());

                uf.setCreateTimeStr(createTimeStr);
                uf.setUpdateTimeStr(updateTimeStr);
            }

            page.setResult(ureportFiles);
            result.put("count", page.getTotalCount());
            result.put("data", ureportFiles);
            result.put("code", "0");
            result.put("msg", "加载完成");
        } catch (Exception e) {
            addFailMessage(result, "查询失败");
            logger.error("查询失败", e);
        }
        return result;
    }

}
